package com.demomaster.quickaccessibility.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickaccessibility.R;

import java.util.List;

import cn.demomaster.quicktreeview.TreeNode;
import cn.demomaster.quicktreeview.TreeViewAdapter;

public class SimpleTreeRecyclerAdapter extends TreeViewAdapter {


    public SimpleTreeRecyclerAdapter(Context context, List<TreeNode> dataList, int defaultExpandLevel, String focusedNodeId) {
        super(context, dataList, defaultExpandLevel, focusedNodeId);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.treelist_item, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new MyHoder(view);
    }

    @Override
    public void onBindViewHolder(final TreeNode node, RecyclerView.ViewHolder holder, int position) {
        final MyHoder viewHolder = (MyHoder) holder;
        viewHolder.cb.setOnClickListener(v -> {
            setChecked(position, viewHolder.cb.isChecked());
        });
        viewHolder.cb.setChecked(node.isChecked());
//        if (node.getIcon() == -1) {
//            viewHolder.icon.setVisibility(View.INVISIBLE);
//        } else {
//            viewHolder.icon.setVisibility(View.VISIBLE);
//            viewHolder.icon.setImageResource(node.getIcon());
//        }

        holder.itemView.setSelected(node.getId().equals(focusedNodeId));
        viewHolder.label.setText(node.getLable());
        holder.itemView.setPadding(node.getLevel() * 10, 3, 3, 3);
    }

    static class MyHoder extends RecyclerView.ViewHolder {
        public CheckBox cb;
        public TextView label;
        public ImageView icon;

        public MyHoder(View itemView) {
            super(itemView);
            cb = itemView.findViewById(R.id.cb_select_tree);
            label = itemView.findViewById(R.id.id_treenode_label);
            icon = itemView.findViewById(R.id.icon);
        }
    }
}
